package com.wudi.estate.controller.servicemode;

import com.wudi.estate.bean.servicemode.Repair;
import com.wudi.estate.service.assetmanage.DeviceService;
import com.wudi.estate.service.communitymanagement.CommunityService;
import com.wudi.estate.service.ownermanagement.PersonnelService;
import com.wudi.estate.service.servicemode.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/repair")
public class RepairController {
    @Autowired
    private RepairService repairService;
    @Autowired
    private CommunityService communityService;
    @Autowired
    private PersonnelService personnelService;
    @Autowired
    private DeviceService deviceService;

    //报修列表显示
    @RequestMapping("/listUi")
    public String listUi(ModelMap map) {
        try {
            List<Map<String, Object>> repairs = repairService.selectAll();
            map.addAttribute("repairs", repairs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "repair_list";
    }
    //    显示添加活动的页面
    @RequestMapping("/addUi")
    public String addUi(ModelMap map) {
        System.out.println(123);
        Map<String, Object> maps = null;
        try {
            maps = repairService.selectRepairAndCommunityAndPersonnel();
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("maps", maps);
        return "repair_add";
    }

    //        添加报修，返回到“报修列表显示”里
    @RequestMapping("/add")
    public String add(Repair repair){//使对象自动注入，id和createTime不需要

        try {
            repairService.insert(repair);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/repair/addUi";
    }
    //   报修编辑显示
    @RequestMapping("/editUi")
    public String editUi(ModelMap map, long id){
        System.out.println(id);
        try {
            Map<String,Object> maps= repairService.selectRepairAndPersonnelAndDeviceById(id);
            System.out.println(maps);
            map.addAttribute("maps",maps);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "repair_edit";
    }
    //    //   报修编辑逻辑
    @RequestMapping("/edit")
    public String edit(Repair repair){
        System.out.println(repair);
        try {
            repairService.update(repair);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/repair/listUi";
    }
    //    报修删除逻辑
    @RequestMapping("/delete")
    public String delete(long id){
        try {
            repairService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(id);
        return "redirect:/repair/listUi";
    }
}
